package com.hksj.Intelligentmanagement.auth.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hksj.Intelligentmanagement.auth.entity.LoginUser;
import com.hksj.Intelligentmanagement.auth.entity.User;
import com.hksj.Intelligentmanagement.auth.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuService menuService;
    @Autowired
    private RoleService roleService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        User user = userMapper.selectOne(wrapper);
        //如果查询不到数据就通过抛出异常来给出提示
        if (Objects.isNull(user)) {
            throw new RuntimeException("用户名或密码错误");
        }
        //查询用户的权限信息
        List<String> permissionKeyList =  menuService.listUserPermissionKey(user.getId());

        //查询用户的角色信息
        List<String> roleKeyList = roleService.listUserRoleKey(user.getId());

        //封装成UserDetails对象返回
        return new LoginUser(user, permissionKeyList,roleKeyList);
    }
}